﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using PPIProxy;
using VMind.Global;
using System.Data;
using PPIDataAccess.Data;

namespace PPIDesktopClient.Logic
{
    public static class LVendor_Visit_XMissionStructures_Pole
    {
        #region CRUD

        public static void Save(EVendor_Visit_XMissionStructures_Pole pole)
        {
            if (pole.Id == Int32.MinValue)
            {
                pole.Id = Insert(pole);
            }
            else
            {
                Update(pole);
            }
        }

        private static int Insert(EVendor_Visit_XMissionStructures_Pole pole)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            int _NewPoleServerID;
            _NewPoleServerID = WVendor_Visit_XMissionStructures_Pole.Insert(pole.Asset.Id, pole.TopType.XMissionStructures_PoleTopTypeId, pole.Species.XMissionStructures_PoleSpeciesId, pole.Treatment.XMissionStructures_PoleTreatmentId, pole.Year, pole.Class.XMissionStructures_PoleClassId, pole.Latitude, pole.Longitude, pole.Altitude, pole.SatelliteCount, pole.SatelliteTime, pole.AID, pole.LVisit, pole.Number, pole.Length, pole.LVisitor, pole.SolutionAvailable,_Reader.GetConnectionString());
            return _NewPoleServerID;
        }

        private static void Update(EVendor_Visit_XMissionStructures_Pole pole)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            WVendor_Visit_XMissionStructures_Pole.Update(pole.Id, pole.Asset.Id, pole.TopType.XMissionStructures_PoleTopTypeId, pole.Species.XMissionStructures_PoleSpeciesId, pole.Treatment.XMissionStructures_PoleTreatmentId, pole.Year, pole.Class.XMissionStructures_PoleClassId, pole.Latitude, pole.Longitude, pole.Altitude, pole.SatelliteCount, pole.SatelliteTime, pole.AID, pole.LVisit, pole.Number, pole.Length, pole.LVisitor, pole.SolutionAvailable,_Reader.GetConnectionString());
        }

        public static int GetTotal()
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            return WVendor_Visit_XMissionStructures_Pole.GetTotal(_Reader.GetConnectionString());
        }

        public static int UploadPole(EVendor_Visit_XMissionStructures_Pole pole, Int32 structureID)
        {
            Int32 _NewPoleServerID;
            ConfigReader _Reader = ConfigReader.GetInstance();
            _NewPoleServerID = WVendor_Visit_XMissionStructures_Pole.Insert(structureID, pole.TopType.XMissionStructures_PoleTopTypeId,
                                                    pole.Species.XMissionStructures_PoleSpeciesId,
                                                    pole.Treatment.XMissionStructures_PoleTreatmentId,
                                                    pole.Year, pole.Class.XMissionStructures_PoleClassId,
                                                    pole.Latitude, pole.Longitude, pole.Altitude, pole.SatelliteCount, pole.SatelliteTime, null, pole.LVisit, pole.Number, pole.Length, pole.LVisitor, pole.SolutionAvailable,_Reader.GetConnectionString());
            /*_NewPoleServerID = DVendor_Visit_XMissionStructures_Pole.Insert(structureID, pole.TopType.XMissionStructures_PoleTopTypeId,
                                                                pole.Species.XMissionStructures_PoleSpeciesId,
                                                                pole.Treatment.XMissionStructures_PoleTreatmentId,
                                                                pole.Year, pole.Class.XMissionStructures_PoleClassId,
                                                                pole.Latitude, pole.Longitude, pole.Altitude, pole.SatelliteCount, pole.SatelliteTime, null, pole.LVisit, pole.Number, pole.Length, pole.LVisitor, pole.SolutionAvailable, _Reader.GetConnectionString());*/


            return _NewPoleServerID;
        }

        public static void Delete(EVendor_Visit_XMissionStructures_Pole p)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            WVendor_Visit_XMissionStructures_Pole.DesasocciateImages(p.Id, _Reader.GetConnectionString());
            WVendor_Visit_XMissionStructures_Pole.Delete(p.Id,_Reader.GetConnectionString());
        }

        #endregion

        #region Query

        public static List<EVendor_Visit_XMissionStructures_Pole> GetByStructureID(int structureID, DataTable imageDataTable)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            DataTable PolesData = WVendor_Visit_XMissionStructures_Pole.GetByStructureID(structureID,_Reader.GetConnectionString()).Tables[0];

            EVendor_Visit_XMissionStructures_Pole _Pole;
            List<EVendor_Visit_XMissionStructures_Pole> _Poles = new List<EVendor_Visit_XMissionStructures_Pole>();

            foreach (DataRow _PoleData in PolesData.Rows)
            {
                #region Poles
                _Pole = new EVendor_Visit_XMissionStructures_Pole();
                _Pole.Id = ConvertHelper.ToInt32(_PoleData["Vendor_Visit_XMissionStructures_PoleId"]);
                _Pole.Asset = new EVendor_Visit_XMissionStructure(structureID);

                _Pole.TopType = new EXMissionStructures_PoleTopType(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleTopTypeFk"]), ConvertHelper.Tostring(_PoleData["Top_Name"]));
                _Pole.Species = new EXMissionStructures_PoleSpecies(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleSpeciesFk"]), ConvertHelper.Tostring(_PoleData["Species_Name"]));
                _Pole.Treatment = new EXMissionStructures_PoleTreatment(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleTreatmentFK"]), ConvertHelper.Tostring(_PoleData["Treatment_Name"]));
                _Pole.Class = new EXMissionStructures_PoleClass(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleClassFK"]), ConvertHelper.Tostring(_PoleData["Classes_Name"]));


                _Pole.Year = ConvertHelper.Toint(_PoleData["Year"]);
                _Pole.Latitude = ConvertHelper.Todouble(_PoleData["Latitude"]);
                _Pole.Longitude = ConvertHelper.Todouble(_PoleData["Longitude"]);
                _Pole.Altitude = ConvertHelper.Todouble(_PoleData["Altitude"]);
                _Pole.SatelliteCount = ConvertHelper.Toint(_PoleData["SatelliteCount"]);
                _Pole.SatelliteTime = ConvertHelper.ToDateTime(_PoleData["SatelliteTime"]);
                _Pole.SolutionAvailable = ConvertHelper.Tobool(_PoleData["SolutionAvailable"]);
                _Pole.AID = ConvertHelper.Tostring(_PoleData["AID"]);
                _Pole.LVisit = ConvertHelper.Toint(_PoleData["LVisit"]);
                _Pole.Number = ConvertHelper.Tostring(_PoleData["Number"]);
                _Pole.Length = ConvertHelper.Todouble(_PoleData["Length"]);
                _Pole.LVisitor = ConvertHelper.Tostring(_PoleData["LVisitor"]);

                List<EVendor_Visit_XMissionStructures_Image> _Imagelist = LVendor_Visit_XMissionStructures_Image.GetByPoleID(_Pole.Id, imageDataTable);
                foreach (EBaseImage i in _Imagelist)
                {
                    if (!_Pole.Images.Contains(i))
                    {
                        _Pole.Images.Add(i);
                    }
                }
                #endregion Poles

                _Poles.Add(_Pole);
            }
            return _Poles;
        }

        public static List<EVendor_Visit_XMissionStructures_Pole> GetByStructureID(EVendor_Visit_XMissionStructure structure, DataTable imageDataTable)
        {
            return GetByStructureID(structure.Id, imageDataTable);
        }

        /// <summary>
        /// Comentario 1.1
        /// </summary>
        /// <param name="structure"></param>
        /// <param name="images"></param>
        public static void AssigningPolesToStructure(EVendor_Visit_XMissionStructure structure, List<EVendor_Visit_XMissionStructures_Image> images)
        {
            ConfigReader _Reader = ConfigReader.GetInstance();
            DataTable PolesData = WVendor_Visit_XMissionStructures_Pole.GetByStructureID(structure.Id,_Reader.GetConnectionString()).Tables[0];

            EVendor_Visit_XMissionStructures_Pole _Pole;

            foreach (DataRow _PoleData in PolesData.Rows)
            {
                #region Poles
                _Pole = new EVendor_Visit_XMissionStructures_Pole();
                _Pole.Id = ConvertHelper.ToInt32(_PoleData["Vendor_Visit_XMissionStructures_PoleId"]);
                _Pole.Asset = structure;

                _Pole.TopType = new EXMissionStructures_PoleTopType(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleTopTypeFk"]), ConvertHelper.Tostring(_PoleData["Top_Name"]));
                _Pole.Species = new EXMissionStructures_PoleSpecies(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleSpeciesFk"]), ConvertHelper.Tostring(_PoleData["Species_Name"]));
                _Pole.Treatment = new EXMissionStructures_PoleTreatment(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleTreatmentFK"]), ConvertHelper.Tostring(_PoleData["Treatment_Name"]));
                _Pole.Class = new EXMissionStructures_PoleClass(ConvertHelper.Toint(_PoleData["XMissionStructures_PoleClassFK"]), ConvertHelper.Tostring(_PoleData["Classes_Name"]));

                _Pole.Year = ConvertHelper.Toint(_PoleData["Year"]);

                _Pole.Latitude = ConvertHelper.Todouble(_PoleData["Latitude"]);
                _Pole.Longitude = ConvertHelper.Todouble(_PoleData["Longitude"]);
                _Pole.Altitude = ConvertHelper.Todouble(_PoleData["Altitude"]);
                _Pole.SatelliteCount = ConvertHelper.Toint(_PoleData["SatelliteCount"]);
                _Pole.SatelliteTime = ConvertHelper.ToDateTime(_PoleData["SatelliteTime"]);
                _Pole.SolutionAvailable = ConvertHelper.Tobool(_PoleData["SolutionAvailable"]);
                _Pole.AID = ConvertHelper.Tostring(_PoleData["AID"]);
                _Pole.LVisit = ConvertHelper.Toint(_PoleData["LVisit"]);
                _Pole.Number = ConvertHelper.Tostring(_PoleData["Number"]);
                _Pole.Length = ConvertHelper.Todouble(_PoleData["Length"]);
                _Pole.LVisitor = ConvertHelper.Tostring(_PoleData["LVisitor"]);
                foreach (EVendor_Visit_XMissionStructures_Image i in images)
                {
                    if (_Pole.Id == i.Pole.Id)
                    {
                        i.IsDesasocciated = false;
                        _Pole.Images.Add(i);
                    }
                }
                #endregion Poles
                structure.Poles.Add(_Pole);
            }
            
        }

        #endregion


    }
}
